from lxml import etree
import json
from selenium import webdriver
import csv
from time import sleep
import random
import os
from pathlib import Path
import pandas as pd
from analysis import open

class data_tidy:
    def __init__(self,filename,names=None,encoding='utf-8'):
        suffix = filename[filename.rfind('.') + 1:]
        if suffix == 'csv':
            self.data = open.open_csv(filename,names = names,encoding=encoding)
        elif suffix == 'xlsx':
            self.data = open.open_excel(filename, names=names, encoding=encoding)

    #去重
    def drop_duplicates(self,inplace=True):
        df = self.data.drop_duplicates(inplace=inplace)
        return df

    #去掉都是nan的行
    def drop_all_na(self,inplace=True):
        self.data.dropna(how='all',inplace=inplace)

    #去重+去掉都是nan的行
    def drop_duplicates_nan(self,inplace=True):
        self.drop_duplicates(inplace=inplace)
        self.drop_all_na(inplace=inplace)

    # 去掉某些列中有空值的行
    def columne_drop_na(self, columne_list, inplace=True):
        df = self.data.dropna(axis=0, subset=columne_list, inplace=inplace)
        # 之前自己写的
        # drop_index = []
        # for columne in columne_list:
        #     missing_index = self.data[self.data[columne].isnull().values == True].index  # 得到columne有缺失值的行的索引，
        #     drop_index = list(set(drop_index)|set(missing_index))
        # df = self.data.drop(drop_index, inplace=inplace)
        return df

    # 按分隔符进行分列操作
    def column_split(self, column_name, split, split_names):
        for name in split_names:
            self.data[name] = None  # 先吧列都插进去，不然后面加不上数据

        for index in self.data.index:
            content = str(self.data[column_name][index])  # 解决部分关键字为数值的问题
            if split in content:  # 解决关键字中没有指定分隔符的问题
                splits = content.split(split)  # 对传入的内容进行分割

                for name_index, new_name in enumerate(split_names):
                    try:
                        self.data[new_name][index] = splits[name_index]
                    except:
                        pass
            else:
                self.data[split_names[0]][index] = content

    # 为一列中的文字进行replace操作
    def column_str_replace(self,column_list, old, new):
        for column in column_list:
            self.data[column] = self.data[column].map(lambda x: x.replace(old, new))

    # 添加一列
    def add_columne(self):
        pass

    # 判断要查找的是否在数据中
    def isin_data(self,find_map):
        return True

    #去掉windows中文件名禁止出现的字符
    def drop_win_forbid(self, columne):
        def drop_forbid(x):
            p = re.compile(r'[-,$()#+&*？/，\\。“”?、·~"\*<>|]')
            s = re.split(p, x)
            return '_'.join(s)
        self.data[columne] = self.data[columne].map(lambda x : drop_forbid(x))
        return self.data
